import request from '@/utils/request'

type Common<T> = {
  code: string
  data: T
  mesg: string
  time: string
}
export type MenuItem = {
  createdBy: string
  createdTime: string
  description: string
  href: string
  icon: string
  id: number
  level: number
  name: string
  operatorId: number | null
  orderNum: number
  parentId: number
  shown: boolean
  updatedBy: string
  updatedTime: string
}
//定义创建或更新菜单所需要的类型   1.partial  2.pick  3不要.omit
//export type CreateOrEditMenu = Partial<MenuItem>
export type CreateOrEditMenu = Pick<
  MenuItem,
  'name' | 'href' | 'parentId' | 'description' | 'icon' | 'shown' | 'orderNum'
> & { id?: number }

// export type CreateOrEditMenu = Omit<
//   MenuItem,
//   'updatedTime' | 'id' | 'parentId' | 'description' | 'icon' | 'shown' | 'orderNum'> & {id?: number}

//1.获取所有菜单
export const getAll = () => {
  return request<Common<MenuItem[]>>({
    method: 'GET',
    url: '/boss/menu/getAll'
  })
}
//2.新增或更新菜单
export const saveOrUpdate = (menuInfo: CreateOrEditMenu) => {
  return request<Common<boolean>>({
    method: 'POST',
    url: '/boss/menu/saveOrUpdate',
    data: menuInfo
  })
}

//3.删除菜单
export const deleteMenu = (id: MenuItem['id']) => {
  return request<Common<boolean>>({
    method: 'DELETE',
    url: `/boss/menu/${id}`
  })
}
